package com.dlshouwen.core.base.mail;

import javax.servlet.http.HttpServletRequest;

import com.dlshouwen.core.base.utils.AttributeUtils;

/**
 * 使用163邮箱服务发送邮件
 * @author cuixubin
 *
 */
public class MailSend {
	/**
	 * 发送邮件
	 * @param senderName 发送者名称(邮箱名)
	 * @param senderMail 发送邮箱的地址
	 * @param senderPwd 发送邮箱的密码
	 * @param receiverMail 接收邮箱地址
	 * @param mailTitle 邮件标题
	 * @param mailContent 邮件内容
	 * @return true-发送成功，false-发送失败
	 */
	public static boolean send(String senderName, String senderMail, String senderPwd, String receiverMail,
			String mailTitle, String mailContent) {
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost("smtp.163.com");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		mailInfo.setUserName(senderName); // 实际发送者姓名
		mailInfo.setPassword(senderPwd);// 实际发送者邮箱密码
		mailInfo.setFromAddress(senderMail); // 实际发送者邮箱地址
		mailInfo.setToAddress(receiverMail); // 设置接受者邮箱地址
		mailInfo.setSubject(mailTitle);
		mailInfo.setContent(mailContent);
		// 这个类主要来发送邮件
		SimpleMailSender sms = new SimpleMailSender();
		try {
			sms.sendTextMail(mailInfo); // 发送文体格式
		} catch (Exception e) {
			return false;
		}

		return true;
	}
	
	/**
	 * 发邮件
	 * @param receiverMail 接收邮箱地址
	 * @param mailTitle 邮件标题
	 * @param mailContent 邮件内容
	 * @return true-发送成功，false-发送失败
	 */
	public static boolean send(HttpServletRequest request, String receiverMail, String mailTitle, String mailContent) {
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost("smtp.163.com");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		String senderName = AttributeUtils.getAttributeContent(request.getServletContext(), "mail_sender_name");
		String mailAddress = AttributeUtils.getAttributeContent(request.getServletContext(), "mail_mail_address");
		String mailPassword = AttributeUtils.getAttributeContent(request.getServletContext(), "mail_mail_password");
		mailInfo.setUserName(senderName); // 实际发送者姓名
		mailInfo.setPassword(mailAddress);// 实际发送者邮箱密码
		mailInfo.setFromAddress(mailPassword); // 实际发送者邮箱地址
		mailInfo.setToAddress(receiverMail); // 设置接受者邮箱地址
		mailInfo.setSubject(mailTitle);
		mailInfo.setContent(mailContent);
		// 这个类主要来发送邮件
		SimpleMailSender sms = new SimpleMailSender();
		try {
			sms.sendTextMail(mailInfo); // 发送文体格式
		} catch (Exception e) {
			return false;
		}

		return true;
	}
}
